function initUpload() {
	var i = $(".img_main").find(".include").length;
	i >= 9 && (addstatus = !1), i <= 0 || i >= 9 ? $(".btn-add").hide() : ($(".btn-add").show(), addstatus = !0), $(".uploadbtn,.addimg_btn").remove(), $(".input-con").append('<input type="file" class="uploadbtn">'), $(".img_add_wr").append('<input class="img_add hide addimg_btn"  type="file">')
}

function postImg(i, t) {
	var a = API_URL + "images",
		e = {
			req_data: {
				type: "photo",
				hash: i
			}
		},
		d = function(i) {
			if(i.succeeded) {
				var a = "",
					e = i.resp_data,
					d = e.image_id;
				e.upload_url ? (a = e.upload_url, postUploadurl(a, t, d, imgName)) : previewPic(t, d, imgName), imgarr.push(d)
			}
		};
	Public.loadAjaxData(a, e, d, null, "post")
}

function previewPic(i, t, a) {
	var e = new FileReader;
	e.onload = function(i) {
		compressImg(i.target.result, t, a)
	}, e.readAsDataURL(i)
}

function postUploadurl(i, t, a, e) {
	var i = i,
		d = t,
		n = function(i) {
			i.succeeded ? previewPic(t, a, e) : Public.showToastByCode(i.code)
		},
		o = new FormData;
	o.append("file", d), Public.loadAjaxData(i, o, n, !1, "post", !1, !0, !1, !1, "status")
}

function templateImg(i, t) {
	imgName = "file" + (new Date).getTime();
	var a = $(".img_ul"),
		e = '<div class="include ' + imgName + '"><div class="in_bg"></div><img style="max-width: 204px;"><span class="hide in_close"></span></div>';
	a.append(e), setstyles($(".include")), initUpload()
}

function clearImgarr(i) {
	for(var t = 0; t < imgarr.length; t++) imgarr[t] == +i && imgarr.splice(t, t + 1)
}

function setstyles(i, t) {
	$(i).css({
		width: conwidth + "px",
		height: conwidth + "px"
	}), $(i).find("img").css({
		"max-width": conwidth + "px",
		"max-height": conwidth + "px"
	}), $(".btn-add").css({
		width: conwidth + "px",
		height: conwidth + "px",
		"background-size": conwidth + "px " + conwidth + "px"
	})
}

function compressImg(i, t, a) {
	if(i) {
		var e = document.createElement("canvas");
		maxHeight = maxHeight > 300 ? maxHeight : 300, e.width = maxHeight, e.height = maxHeight;
		var d = new Image;
		d.src = i, d.onload = function() {
			var i, n = d.height,
				o = d.width,
				r = 0,
				s = 0;
			n > o ? (i = o, r = (n - i) / 2) : (i = n, s = (o - i) / 2);
			var l = e.getContext("2d");
			l.fillStyle = "#fff", l.fillRect(0, 0, e.width, e.height), l.drawImage(d, s, r, i, i, 0, 0, maxHeight, maxHeight);
			var c = $("." + a);
			c.find("img").attr("src", e.toDataURL("image/jpeg")), c.find(".in_close").attr("data-id", t).show(), $(".in_bg").hide()
		}
	}
}
var imgarr = [],
	addstatus = !0,
	ww = $(".wrap").width(),
	conwidth = Math.floor((ww - 60) / 3),
	maxHeight = conwidth,
	imgName = "",
	typearr = ["jpg", "jpeg", "png"];
$("body").on("change", ".uploadbtn,.addimg_btn", function() {
	if(file = this.files[0]) {
		var i = file.type.toLowerCase();
		i = i.substr(6, i.length);
		for(var t = !1, a = 0; a < typearr.length; a++) typearr[a] == i && (t = !0);
		if(!t) return void Public.showToast("不支持该文件类型");
		templateImg(file), postImg("1234567890123456789012345678901234567890123456789012345678901234", file)
	}
}), $("body").on("tap", ".in_close", function() {
	$(this).parents(".include").remove(), clearImgarr($(this).attr("data-id")), initUpload()
});